package com.ruoyi.businessInfo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.businessInfo.domain.BedConfig;
import com.ruoyi.businessInfo.mapper.RoomTypeMapper;
import com.ruoyi.businessInfo.domain.RoomType;
import com.ruoyi.businessInfo.service.IRoomTypeService;

/**
 * 房型信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-17
 */
@Service
public class RoomTypeServiceImpl implements IRoomTypeService
{
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    /**
     * 查询房型信息
     *
     * @param roomTypeId 房型信息主键
     * @return 房型信息
     */
    @Override
    public RoomType selectRoomTypeByRoomTypeId(Long roomTypeId)
    {
        return roomTypeMapper.selectRoomTypeByRoomTypeId(roomTypeId);
    }

    /**
     * 查询房型信息列表
     *
     * @param roomType 房型信息
     * @return 房型信息
     */
    @Override
    public List<RoomType> selectRoomTypeList(RoomType roomType)
    {
        return roomTypeMapper.selectRoomTypeList(roomType);
    }

    /**
     * 新增房型信息
     *
     * @param roomType 房型信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRoomType(RoomType roomType)
    {
        roomType.setCreateTime(DateUtils.getNowDate());
        int rows = roomTypeMapper.insertRoomType(roomType);
        insertBedConfig(roomType);
        return rows;
    }

    /**
     * 修改房型信息
     *
     * @param roomType 房型信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRoomType(RoomType roomType)
    {
        roomType.setUpdateTime(DateUtils.getNowDate());
        roomTypeMapper.deleteBedConfigByRoomTypeId(roomType.getRoomTypeId());
        insertBedConfig(roomType);
        return roomTypeMapper.updateRoomType(roomType);
    }

    /**
     * 批量删除房型信息
     *
     * @param roomTypeIds 需要删除的房型信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRoomTypeByRoomTypeIds(Long[] roomTypeIds)
    {
        roomTypeMapper.deleteBedConfigByRoomTypeIds(roomTypeIds);
        return roomTypeMapper.deleteRoomTypeByRoomTypeIds(roomTypeIds);
    }

    /**
     * 删除房型信息信息
     *
     * @param roomTypeId 房型信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRoomTypeByRoomTypeId(Long roomTypeId)
    {
        roomTypeMapper.deleteBedConfigByRoomTypeId(roomTypeId);
        return roomTypeMapper.deleteRoomTypeByRoomTypeId(roomTypeId);
    }

    /**
     * 新增床型配置信息
     *
     * @param roomType 房型信息对象
     */
    public void insertBedConfig(RoomType roomType)
    {
        List<BedConfig> bedConfigList = roomType.getBedConfigList();
        Long roomTypeId = roomType.getRoomTypeId();
        if (StringUtils.isNotNull(bedConfigList))
        {
            List<BedConfig> list = new ArrayList<BedConfig>();
            for (BedConfig bedConfig : bedConfigList)
            {
                bedConfig.setRoomTypeId(roomTypeId);
                list.add(bedConfig);
            }
            if (list.size() > 0)
            {
                roomTypeMapper.batchBedConfig(list);
            }
        }
    }
}
